package com.dbappsecurity.jsbridge;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetDialog;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.dbappsecurity.jsbridge.Interface.JsPicDownloadListener;
import java.lang.ref.WeakReference;

/**
 * Created by Arirus on 2017/8/3.
 */

public class JsCustomDialog extends BottomSheetDialog {

  public Activity activity;
  public Button mButton1;
  public Button mButton2;
  public Button mButton3;

  private static String sType;
  private static String sExtra;
  private static WeakReference<JsCustomDialog> sDialog;
  private static WeakReference<JsPicDownloadListener> sPicDownloadInter;

  private JsCustomDialog(@NonNull Context context) {
    super(context);
    activity = (Activity) context;
  }

  public static void start(Context context, String type, String extra, JsPicDownloadListener picDownloadInter) {
    sType = type;
    sExtra = extra;
    sPicDownloadInter = new WeakReference<JsPicDownloadListener>(picDownloadInter);
    if (sDialog == null || sDialog.get() == null) {
      sDialog = new WeakReference<JsCustomDialog>(new JsCustomDialog(context));
    } else {
      sDialog.get().refreshType();
    }
    sDialog.get().show();
  }

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.js_customdialog);
    mButton1 = (Button) findViewById(R.id.js_dialog_btn_1);
    mButton2 = (Button) findViewById(R.id.js_dialog_btn_2);
    mButton3 = (Button) findViewById(R.id.js_dialog_btn_3);
    //针对BottomSheetDialog弹出时，状态栏变黑
    int screenHeight = getScreenHeight();
    int statusBarHeight = getStatusBarHeight(getContext());
    int dialogHeight = screenHeight - statusBarHeight;
    getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
        dialogHeight == 0 ? ViewGroup.LayoutParams.MATCH_PARENT : dialogHeight);
    refreshType();
  }

  private int getScreenHeight() {
    DisplayMetrics displaymetrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    return displaymetrics.heightPixels;
  }

  private int getStatusBarHeight(Context context) {
    int statusBarHeight = 0;
    if(context == null) return statusBarHeight ;
    Resources res = context.getResources();
    int resourceId = res.getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
      statusBarHeight = res.getDimensionPixelSize(resourceId);
    }
    return statusBarHeight;
  }

  JsPicDownloadListener mPicDownloadInter = null;

  private void refreshType() {
    switch (sType) {
      case "1":
        mButton1.setVisibility(View.VISIBLE);
        mButton1.setText("新建窗口打开");
        mButton1.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(sExtra));
            getContext().startActivity(intent);
            sDialog.get().dismiss();
          }
        });
        mButton2.setVisibility(View.VISIBLE);
        mButton2.setText("下载图片");
        mButton2.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            sPicDownloadInter.get().download(getContext(),sExtra);
            sDialog.get().dismiss();
          }
        });
        mButton3.setVisibility(View.GONE);
        break;
      case "2":
        mButton1.setVisibility(View.VISIBLE);
        mButton1.setText("浏览器打开");
        mButton1.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(sExtra));
            getContext().startActivity(intent);
            sDialog.get().dismiss();
          }
        });
        mButton2.setVisibility(View.VISIBLE);
        mButton2.setText("复制到剪贴板");
        mButton2.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            ClipboardManager cm = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
            // 将文本内容放到系统剪贴板里。
            cm.setPrimaryClip(ClipData.newPlainText("label",sExtra));
            Toast.makeText(getContext(), "复制成功。", Toast.LENGTH_LONG).show();
            sDialog.get().dismiss();
          }
        });
        mButton3.setVisibility(View.VISIBLE);
        mButton3.setText("分享");
        mButton3.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.putExtra(Intent.EXTRA_TEXT, sExtra);
            intent.setType("text/plain");
            getContext().startActivity(intent);
            sDialog.get().dismiss();
          }
        });
        break;
      default:
        mButton1.setVisibility(View.VISIBLE);
        mButton2.setVisibility(View.VISIBLE);
        mButton3.setVisibility(View.VISIBLE);
        break;
    }
  }
}
